using Gameplay.PVE.Config;
using Gameplay.PVE.Entity;
using UnityEngine;

namespace Gameplay.PVE
{
    public class ChangePropertyElement : ElementBase
    {
        private float value;
        private float value2;

        private int lastSearchType;
        private int lastSearchSort;
        private int lastSearchChangeType;

        private float GetOutput()
        {
            return PveOutputFunction.GetOutput(config.id, source.Data, target.Data,skill.level);
        }
        
        public override void Execute()
        {
            base.Execute();
            int attributeId = GetValue("AttributeID");
            int calculateType = GetValue("CalculateType");
            if (calculateType == 0)
            {
                calculateType = 1;
            }
            if (attributeId == 1)//攻击
            {
                target.Data.AttackValue += GetChangeValue(target.Data.AttackValue);
            }
            else if (attributeId == 2)//防御
            {
                target.Data.DefenceValue += GetChangeValue(target.Data.DefenceValue);
            }
            else if (attributeId == 6)//暴击率
            {
                target.Data.CriticalRate += GetChangeValue(target.Data.CriticalRate);
            }
            else if (attributeId == 7) //攻速
            {
                target.Data.propertyAttackSpeed += GetChangeValue(target.Data.propertyAttackSpeed);
            }
            else if (attributeId == 8) //改变弹药数量
            {
                target.Data.leftAmmoCount += (int)GetChangeValue(target.Data.leftAmmoCount);
            }
            else if (attributeId == 9) //闪避
            {
                target.Data.evade += GetChangeValue(target.Data.evade);
            }
            else if (attributeId == 10) //加生命上限
            {
                var previousPercent = target.Data.CurrentHp / target.Data.TotalHp;
                value = GetChangeValue(target.Data.TotalHp);
                target.Data.TotalHp += value;
                target.Data.CurrentHp = previousPercent * target.Data.TotalHp;
                target.Transmit(ETransmitType.UpdateHp);
            }
            else if (attributeId == 11) //普攻伤害减免
            {
                target.Data.NormalAttackDamageReduce += GetChangeValue(target.Data.NormalAttackDamageReduce);
            }
            else if (attributeId == 12) //技能伤害减免
            {
                target.Data.SkillDamageReduce += GetChangeValue(target.Data.SkillDamageReduce);
            }
            else if (attributeId == 13) // 性别伤害减免
            {
                var targetGender = GetValue("TargetGender");
                target.Data.GenderDamageReduce[targetGender] +=
                    GetChangeValue(target.Data.GenderDamageReduce[targetGender]);
            }
            else if (attributeId == 14) // 反伤
            {
                target.Data.Thorns += GetChangeValue(target.Data.Thorns);
            }
            else if (attributeId == 15) //格挡
            {
                value = GetValue("BlockRate") / 10000f;
                var chance = GetValue("BlockChance") / 10000f;
                target.Data.blockList.Add(new Vector2(chance,value));
            }
            else if (attributeId == 16) // 增伤
            {
                target.Data.DamageBonus += GetChangeValue(target.Data.DamageBonus);
            }
            else if (attributeId == 17) // 普攻增伤
            {
                target.Data.NormalAttackDamageBonus += GetChangeValue(target.Data.NormalAttackDamageBonus);
            }
            else if (attributeId == 18) //技能增伤
            {
                target.Data.SkillDamageBonus += GetChangeValue(target.Data.SkillDamageBonus);
            }
            else if (attributeId == 19) // 暴击伤害
            {
                target.Data.CriticalBonus += GetChangeValue(target.Data.CriticalBonus);
            }
            else if (attributeId == 20) // 伤害减免
            {
                target.Data.DamageReduce += GetChangeValue(target.Data.DamageReduce);
            }
            else if (attributeId == 21) //暴击伤害减少
            {
                target.Data.CriticalReduce += GetChangeValue(target.Data.CriticalReduce);
            }
            else if (attributeId == 22) //吸血系数增加
            {
                target.Data.SuckBloodRatio += GetChangeValue(target.Data.SuckBloodRatio);
            }
            else if (attributeId == 23)//治疗
            {
                target.Data.HealBonus += GetChangeValue(target.Data.HealBonus);
            }
            else if (attributeId == 89)//可否被选中
            {
                target.Data.selectable = false;
            }
            else if (attributeId == 90)//更换索敌类型
            {
                var searchSort = GetValue("SearchSort");
                var searchChangeType = GetValue("SearchChangeType");
                var searchType = GetValue("SearchType");
                lastSearchSort = target.Data.searchSort;
                //lastSearchType = target.Data.searchType;
                lastSearchChangeType = target.Data.searchChangeType;
                target.Data.searchSort = searchSort;
                target.Data.searchTypeRpg = searchType;
                target.Data.searchChangeType = searchChangeType;
            }
        }

        private float GetChangeValue(float property)
        {
            int calculateType = GetValue("CalculateType");
            if (calculateType == 0)
            {
                calculateType = 1;
            }
            if (calculateType == 1)
            {
                value = GetOutput();
            }
            else if (calculateType == 2)
            {
                value = GetOutput() - property;
            }
            else if (calculateType == 3)
            {
                value = GetOutput() * property;
            }
            return value;
        }
        
        
        public override void Free()
        {
            int attributeId = GetValue("AttributeID");
            int calculateType = GetValue("CalculateType");
            if (attributeId == 1)
            {
                target.Data.AttackValue -= value;
            }
            else if (attributeId == 2)
            {
                target.Data.DefenceValue -= value;
            }
            else if (attributeId == 6)
            {
                target.Data.CriticalRate -= value;
            }
            else if (attributeId == 7) //攻速
            {
                target.Data.propertyAttackSpeed -= value;
            }
            else if (attributeId == 8) //改变弹药数量
            {
                target.Data.leftAmmoCount -=(int)value;
            }
            else if (attributeId == 9) //闪避
            {
                target.Data.evade -= value;
            }
            else if (attributeId == 10) //加生命上限
            {
                var previousPercent = target.Data.CurrentHp / target.Data.TotalHp;
                target.Data.TotalHp -= value;
                target.Data.CurrentHp = previousPercent * target.Data.TotalHp;
                target.Transmit(ETransmitType.UpdateHp);
            }
            else if (attributeId == 11) //普攻伤害减免
            {
                target.Data.NormalAttackDamageReduce -= value;
            }
            else if (attributeId == 12) //技能伤害减免
            {
                target.Data.SkillDamageReduce -= value;
            }
            else if (attributeId == 13) // 性别伤害减免
            {
                var targetGender = GetValue("TargetGender");
                target.Data.GenderDamageReduce[targetGender] -= value;
            }
            else if (attributeId == 14) // 反伤
            {
                target.Data.Thorns -= value;
            }
            else if (attributeId == 15) //格挡
            {
                var chance = GetValue("BlockChance") / 10000f;
                target.Data.blockList.Remove(new Vector2(chance,value));
            }
            else if (attributeId == 16) // 增伤
            {
                target.Data.DamageBonus -= value;
            }
            else if (attributeId == 17) // 普攻增伤
            {
                target.Data.NormalAttackDamageBonus -= value;
            }
            else if (attributeId == 18) //技能增伤
            {
                target.Data.SkillDamageBonus -= value;
            }
            else if (attributeId == 19) // 暴击伤害
            {
                target.Data.CriticalBonus -= value;
            }
            else if (attributeId == 20) // 伤害减免
            {
                target.Data.DamageReduce -= value;
            }
            else if (attributeId == 21) //暴击伤害减少
            {
                target.Data.CriticalReduce -= value;
            }
            else if (attributeId == 22) //吸血系数增加
            {
                target.Data.SuckBloodRatio -= value;
            }
            else if (attributeId == 23)//治疗
            {
                target.Data.HealBonus -= value;
            }
            else if (attributeId == 89)//可否被选中
            {
                target.Data.selectable = true;
            }
            else if (attributeId == 90) //更改索敌
            {
                target.Data.searchSort = lastSearchSort;
                target.Data.searchTypeRpg = lastSearchType;
                target.Data.searchChangeType = lastSearchChangeType;
            }
            base.Free();
        }
    }
}